﻿package Cain.MkMc 
{
	import flash.display.BlendMode;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	import com.greensock.TweenLite;
	/**
	 * loading文字
	 * @author Cain
	 */
	public class LoadingWd extends Sprite
	{
		private var textfild:TextField;
		private var timer:Timer;
		private var i:int;
		private var str:String;
		private var autoMd:Boolean;
		/**
		 * loading文字
		 * @param	_str 文字內容
		 * @param	_autoMd 自動置中
		 */
		public function LoadingWd(_str:String =  "Now Loading", _autoMd:Boolean = true) 
		{
			autoMd = _autoMd;
			str = _str;
			setup();
		}
		
		private function setup():void
		{
			blendMode = BlendMode.LAYER;
			alpha = 0;
			textfild = new TextField();
			textfild.autoSize = TextFieldAutoSize.LEFT;
			textfild.textColor = 0xffffff;
			textfild.selectable = false;
			textfild.text = str + "...";
			addEventListener(Event.ADDED_TO_STAGE, addthis);
		}
		
		private function addthis(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, addthis);
			addEventListener(Event.REMOVED_FROM_STAGE, renovethis);
			init();
		}
		
		private function init():void
		{
			addChild(textfild);
			if (autoMd) 
			{
				textfild.x = parent.width / 2 - width/2;
				textfild.y = parent.height / 2 - height/2;
			}else 
			{
				textfild.x = - width/2;
				textfild.y = - height/2;
			}
			TweenLite.to ( this, .5, { alpha:1 } );
			timer = new Timer(100);
			timer.addEventListener(TimerEvent.TIMER, onTimer);
			timer.start();
		}
		
		private function onTimer(e:TimerEvent):void 
		{
			i++;
			if (i%4==1) 
			{
				textfild.text = str;
			}else if (i%4==2) 
			{
				textfild.text = str+".";
			}else if (i%4==3) 
			{
				textfild.text = str+"..";
			}else if (i%4==0) 
			{
				textfild.text = str+"...";
			}
		}
		
		private function renovethis(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, renovethis);
			timer.removeEventListener(TimerEvent.TIMER, onTimer);
		}
	}
	
}